package com.gcb.swing.copy.alpha;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

import com.gcb.lucene.FileIndexer;

public class DataHandler extends Thread{
	
	private String src;
	private String desc;
	
	public DataHandler(String srcF, String descF){
		src = srcF;
		desc = descF;
	}

	public static List array2List(Object[] objs){
		List list = new ArrayList();
		for (Object object : objs) {
			list.add(object);
		}
		return list;
	}
	
	public static void copy2(String src, String desc) throws Exception{
		
		File srcFile = new File(src);
		File descFile = new File(desc);
		if(descFile.isDirectory()){
			descFile = new File(desc, srcFile.getName());
		}
		long srcFileLength = srcFile.length();
		int bbNum = (int) ((srcFileLength / 10240000) + 1);
		long st = System.currentTimeMillis();
		FileInputStream fis = new FileInputStream(srcFile);
		FileOutputStream fos = new FileOutputStream(descFile);
		FileChannel fic = fis.getChannel();
		FileChannel foc = fos.getChannel();
		for (int i = 0; i < bbNum; i++) {
			ByteBuffer dst = ByteBuffer.allocate(10240000);
			fic.read(dst);
			dst.flip();
			foc.write(dst);
		}
		System.out.println("总共花费时间：" + (System.currentTimeMillis() - st) + "毫秒！");
		fic.close();
		foc.close();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			copy2(src, desc);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void fileIndex(String src, String desc){
		FileIndexer fi = new FileIndexer();
		File srcF = new File(src);
		File descF = new File(desc);
		if(!srcF.exists())
			srcF.mkdirs();
		if(!descF.exists())
			descF.mkdirs();
		fi.fileIndex(srcF, descF);
	}
	
	public static List fileSearch(String src, String queryString, int queryCount){
		FileIndexer fi = new FileIndexer();
		File srcF = new File(src);
		if(!srcF.exists())
			srcF.mkdirs();
		List list = fi.fileSearch(srcF, queryString, queryCount);
		return list;
	}
	
}
